﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoderJoe
{
    internal class RootDisposable
        : IDisposable
    {
        protected ChildDisposable MyChild { get; private set; }

        public ChildDisposable GetMyChild()
        {
            if(MyChild == null)
                MyChild = new ChildDisposable();

            return MyChild;
        }

        #region IDisposable Members

        protected void Dispose(bool disposing)
        {
            if (MyChild != null)
            {
                MyChild.Dispose();
                MyChild = null;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~RootDisposable()
        {
            Dispose(false);
        }

        #endregion
    }
}
